﻿package blob.levelstates
{
    import blob.*;
    import blob.characters.*;
    import blob.mechanisms.*;
    import blob.ogmo.*;
    import flash.net.*;
    import flash.utils.*;
    import org.flixel.*;

    public class PlayState extends FlxState
    {
        public var _map1:FlxTilemap;
        public var finishCharSize:int;
        public var nextLevelClass:Class;
        public var thisLevelClass:Class;
        public var activeChar:int;
        public var curChars:Array;
        public var indicator:Indicator;
        public var characterGroup:FlxGroup;
        public var collidableMechanisms:FlxGroup;
        public var movingBlocks:FlxGroup;
        public var blocks:FlxGroup;
        public var mechanisms:FlxGroup;
        public var doors:FlxGroup;
        public var switchIsHit:Boolean;
        public var buttonPairs:Dictionary;
        public var totalTime:Number;
        public var threeStarTime:int;
        public var twoStarTime:int;
        public var fanIsHit:Boolean;
        public var pause:PauseMenu;
        public var QID:int;
        public var loggingCounter:int = 1;
        public var timeText:FlxText;
        public var levelName:FlxText;
        private var SO:SharedObject;
        public var idleCounter:Number = 0;
        public var idleTime:Number = 0;
        private var gameMusic:Class;
        private var SoundJump:Class;
        private var SoundButton:Class;
        private var SoundSplit:Class;
        private var SoundMerge:Class;
        private var SoundFan:Class;

        public function PlayState()
        {
            this.gameMusic = PlayState_gameMusic;
            this.SoundJump = PlayState_SoundJump;
            this.SoundButton = PlayState_SoundButton;
            this.SoundSplit = PlayState_SoundSplit;
            this.SoundMerge = PlayState_SoundMerge;
            this.SoundFan = PlayState_SoundFan;
            return;
        }// end function

        override public function create() : void
        {
            this.totalTime = 0;
            this.mechanisms = new FlxGroup();
            this.collidableMechanisms = new FlxGroup();
            this.blocks = new FlxGroup();
            this.characterGroup = new FlxGroup();
            this.doors = new FlxGroup();
            this.movingBlocks = new FlxGroup();
            this.buttonPairs = new Dictionary();
            this.pause = new PauseMenu();
            var _loc_1:* = FlxG.play(this.gameMusic, 0.25, true, true);
            this.levelName = new FlxText(10, FlxG.height - 20, FlxG.width - 50);
            this.levelName.setFormat(null, 13);
            _loc_1.fadeIn(2);
            super.create();
            return;
        }// end function

        public function loadMap(param1:String, param2:FlxTilemap, param3:Class, param4:int, param5:int) : void
        {
            var _loc_6:* = new OgmoLevel(param1);
            new OgmoLevel(param1).loadTiles(param2, param3, param4, param5);
            add(param2);
            _loc_6.loadSprites(this.curChars, this.mechanisms);
            this.indicator = new Indicator(0, 0);
            add(this.indicator);
            this.timeText = new FlxText(750, 580, 150, "");
            this.timeText.setFormat(null, 10);
            add(this.timeText);
            add(this.levelName);
            if (Blob.CID >= 0)
            {
                Blob.getLogger().logLevelStart(this.QID, null);
            }
            return;
        }// end function

        private function splitChar(param1:Character) : void
        {
            var _loc_2:* = param1.split();
            if (_loc_2.length > 1)
            {
                FlxG.play(this.SoundSplit);
            }
            this.remove(this.curChars[this.activeChar]);
            this.curChars.splice(this.activeChar, 1);
            this.curChars = this.curChars.concat(_loc_2);
            this.activeChar = this.curChars.length - 1;
            for each (param1 in _loc_2)
            {
                
                this.add(param1);
            }
            return;
        }// end function

        private function toggleChar() : void
        {
            this.curChars[this.activeChar].stopWalking();
            this.activeChar = (this.activeChar + 1) % this.curChars.length;
            return;
        }// end function

        private function handleAnimations(param1:Character) : void
        {
            return;
        }// end function

        override public function update() : void
        {
            var _loc_2:Boolean = false;
            var _loc_3:int = 0;
            var _loc_4:Character = null;
            var _loc_5:Character = null;
            if (!this.pause.showing)
            {
                this.totalTime = this.totalTime + FlxG.elapsed;
                if (!FlxG.keys.any())
                {
                    if (this.idleCounter >= 30)
                    {
                        this.idleTime = this.idleTime + FlxG.elapsed;
                    }
                    else
                    {
                        this.idleCounter = this.idleCounter + FlxG.elapsed;
                    }
                }
                else
                {
                    this.idleCounter = 0;
                }
            }
            var _loc_1:* = FlxU.formatTime(this.totalTime, true);
            this.timeText.text = _loc_1.substr(0, (_loc_1.length - 1));
            if (5 * this.loggingCounter <= this.totalTime)
            {
                var _loc_6:String = this;
                var _loc_7:* = this.loggingCounter + 1;
                _loc_6.loggingCounter = _loc_7;
                if (Blob.CID >= 0)
                {
                    Blob.getLogger().logAction(Blob.timeActionId, {time:this.totalTime, x:this.curChars[this.activeChar].x, y:this.curChars[this.activeChar].y, size:this.curChars[this.activeChar].size()});
                }
            }
            if (!this.pause.showing)
            {
                this.switchIsHit = false;
                this.fanIsHit = false;
                this.collideCharactersWithMap();
                if (FlxG.keys.justPressed("SPACE"))
                {
                    _loc_2 = false;
                    _loc_3 = 0;
                    while (_loc_3 < this.curChars.length)
                    {
                        
                        if (_loc_3 != this.activeChar && this.curChars[_loc_3].overlaps(this.curChars[this.activeChar]))
                        {
                            _loc_4 = this.curChars[this.activeChar].merge(this.curChars[_loc_3]);
                            _loc_5 = this.curChars[this.activeChar];
                            if (_loc_4 == null)
                            {
                                _loc_2 = true;
                                break;
                            }
                            FlxG.play(this.SoundMerge);
                            this.add(_loc_4);
                            this.remove(this.curChars[_loc_3]);
                            this.remove(this.curChars[this.activeChar]);
                            if (_loc_3 > this.activeChar)
                            {
                                this.curChars.splice(_loc_3, 1);
                                this.curChars.splice(this.activeChar, 1);
                            }
                            else
                            {
                                this.curChars.splice(this.activeChar, 1);
                                this.curChars.splice(_loc_3, 1);
                            }
                            this.activeChar = this.curChars.push(_loc_4) - 1;
                            _loc_2 = true;
                            break;
                        }
                        _loc_3++;
                    }
                    if (!_loc_2)
                    {
                        this.splitChar(this.curChars[this.activeChar]);
                    }
                    this.bringMechanismsToFront();
                }
                else if (FlxG.keys.justPressed("TAB") || FlxG.keys.justPressed("Q"))
                {
                    this.toggleChar();
                }
                else if (FlxG.keys.justPressed("P"))
                {
                    this.bringMechanismsToFront();
                    this.pause = new PauseMenu();
                    this.pause.showPaused();
                    add(this.pause);
                }
                this.checkLevelFinished();
                _loc_3 = 0;
                while (_loc_3 < this.curChars.length)
                {
                    
                    if (this.curChars[_loc_3].x + this.curChars[_loc_3].width >= FlxG.width)
                    {
                        this.curChars[_loc_3].x = FlxG.width - this.curChars[_loc_3].width;
                    }
                    _loc_3++;
                }
                this.updateCharacter(this.curChars[this.activeChar]);
                this.indicator.x = this.curChars[this.activeChar].x + this.curChars[this.activeChar].width / 2 - this.indicator.width / 2;
                this.indicator.y = this.curChars[this.activeChar].y - 2 * this.indicator.height;
                super.update();
            }
            else
            {
                this.pause.update();
            }
            return;
        }// end function

        public function bringMechanismsToFront() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this.curChars.length)
            {
                
                remove(this.curChars[_loc_1]);
                add(this.curChars[_loc_1]);
                _loc_1++;
            }
            remove(this.collidableMechanisms);
            remove(this.mechanisms);
            add(this.collidableMechanisms);
            add(this.mechanisms);
            return;
        }// end function

        public function updateCharacter(param1:Character) : void
        {
            if (FlxG.keys.A || FlxG.keys.LEFT)
            {
                param1.facing = FlxObject.LEFT;
                param1.startWalking(true);
            }
            else if (FlxG.keys.D || FlxG.keys.RIGHT)
            {
                param1.facing = FlxObject.RIGHT;
                param1.startWalking(false);
            }
            else if (FlxG.keys.justReleased("A") || FlxG.keys.justReleased("LEFT") || FlxG.keys.justReleased("D") || FlxG.keys.justReleased("RIGHT"))
            {
                param1.stopWalking();
            }
            if ((FlxG.keys.justPressed("W") || FlxG.keys.justPressed("UP")) && param1.velocity.y == 0)
            {
                FlxG.play(this.SoundJump, 1, false, true);
                param1.velocity.y = -param1.jumpPower;
            }
            if (FlxG.keys.justPressed("R"))
            {
                if (Blob.CID >= 0)
                {
                    Blob.getLogger().logAction(Blob.resetActionId, {time:this.totalTime, x:this.curChars[this.activeChar].x, y:this.curChars[this.activeChar].y, size:this.curChars[this.activeChar].size()});
                }
                FlxG.resetState();
            }
            if (FlxG.keys.justPressed("M"))
            {
                FlxG.switchState(new GameMenu());
                this.kill();
                this.pause.kill();
            }
            return;
        }// end function

        public function collideCharactersWithMap() : void
        {
            var _loc_1:Object = null;
            var _loc_2:Character = null;
            var _loc_3:Button = null;
            var _loc_4:Character = null;
            for (_loc_1 in this.buttonPairs)
            {
                
                _loc_3 = _loc_1 as Button;
                _loc_4 = this.buttonPairs[_loc_1] as Character;
                if (!FlxG.collide(_loc_4, _loc_3))
                {
                    _loc_3.deactivate();
                    delete this.buttonPairs[_loc_1];
                }
            }
            for each (_loc_2 in this.curChars)
            {
                
                FlxG.collide(_loc_2, this._map1);
                FlxG.collide(this.mechanisms, this._map1);
                FlxG.collide(this.collidableMechanisms, this.collidableMechanisms);
                FlxG.collide(_loc_2, this.collidableMechanisms, this.activateMech);
                FlxG.collide(this.blocks, this._map1);
                if (_loc_2.size() >= 2)
                {
                    FlxG.collide(_loc_2, this.blocks, this.activateBlock);
                }
                else
                {
                    FlxG.overlap(_loc_2, this.blocks, this.activateBlock);
                }
                FlxG.collide(this.movingBlocks, this._map1);
                FlxG.collide(this.movingBlocks, _loc_2);
                FlxG.collide(this.movingBlocks, this.movingBlocks);
                FlxG.collide(this.blocks, this.blocks);
                FlxG.collide(this.blocks, this.movingBlocks);
                FlxG.collide(this.blocks, this.doors);
                FlxG.collide(this.movingBlocks, this.doors);
                FlxG.collide(this.doors, _loc_2);
                FlxG.collide(this.collidableMechanisms, this._map1);
                FlxG.collide(this.collidableMechanisms, this.doors);
                FlxG.overlap(_loc_2, this.mechanisms, this.activateMech);
            }
            return;
        }// end function

        public function activateMech(param1:Character, param2:Mechanism) : void
        {
            var _loc_3:FlxSound = null;
            var _loc_4:Button = null;
            var _loc_5:MovableBlock = null;
            if (param2 is Switch && !this.switchIsHit && param1 == this.curChars[this.activeChar] && (FlxG.keys.justPressed("S") || FlxG.keys.justPressed("DOWN")))
            {
                param2.activate(param1);
                FlxG.play(this.SoundButton);
                this.switchIsHit = true;
            }
            else if (param2 is Fan && !this.fanIsHit)
            {
                if (param1.velocity.y < 0 && param1.velocity.y > -60)
                {
                    _loc_3 = FlxG.play(this.SoundFan, 1, false, true);
                    _loc_3.fadeIn(0.5);
                    _loc_3.fadeOut(0.5);
                }
                param2.activate(param1);
                this.fanIsHit = true;
            }
            else if (param2 is Button)
            {
                _loc_4 = param2 as Button;
                if (!_loc_4.isActivated() && this.buttonPairs[param1] != _loc_4)
                {
                    param2.activate(param1);
                }
            }
            else if (param2 is MovableBlock)
            {
                _loc_5 = param2 as MovableBlock;
                _loc_5.Lock();
                if (param1.size() >= 2)
                {
                    param2.activate(param1);
                }
            }
            else if (!(param2 is Switch))
            {
                param2.activate(param1);
            }
            return;
        }// end function

        public function activateBlock(param1:Character, param2:MovableBlock) : void
        {
            this.movingBlocks.add(param2);
            if (param1.size() >= 2)
            {
                param2.activate(param1);
            }
            else
            {
                param2.Lock();
                param2.activate(param1);
            }
            return;
        }// end function

        public function checkLevelFinished() : void
        {
            if (this.curChars[this.activeChar].x + this.curChars[this.activeChar].width >= FlxG.width && this.curChars[this.activeChar].size() == this.finishCharSize)
            {
                if (Blob.CID >= 0)
                {
                    Blob.getLogger().logLevelEnd({QID:this.QID, finishTime:this.totalTime, idleTime:this.idleTime});
                }
                FlxG.switchState(new FinishedLevelState(this.thisLevelClass, this.nextLevelClass, this.totalTime, this.twoStarTime, this.threeStarTime, this.QID));
                this.pause.kill();
                this.kill();
            }
            return;
        }// end function

    }
}
